use std::{collections::HashMap, hash::Hash};

use model_graph_types::{
    database::DatabaseType,
    generator::{CodeGenerator, CodeGeneratorOption, CodeLanguage},
    modeling::{Class, Value},
};

use generator_engine::tera::TeraEngine;

/// 通过类的信息获取到查询语句
pub async fn get_select_by_class(class: &Class) -> anyhow::Result<String> {
    let template = include_str!("select.tera");

    let engine = TeraEngine {};

    let sql = engine
        .generate_string(
            CodeLanguage::Sql(DatabaseType::SQLite),
            class.clone().try_into()?,
            template,
            CodeGeneratorOption {},
        )
        .await?;

    Ok(sql)
}

fn _get_table_name(class: &Class) -> anyhow::Result<String> {
    Ok(class.identifier.clone())
}
fn _get_attributes(class: &Class) -> anyhow::Result<Value> {
    let attributes = class
        .attributes
        .iter()
        .map(|a| {
            let map = HashMap::<String, Value>::new();

            Value::Object(map)
        })
        .collect::<Vec<Value>>();
    Ok(Value::Array(attributes))
}
